package game.tileMap.model {
	import game.tileMap.model.vo.Unit;
	
	
	/**
	 * @author GUNDAM
	 * // 速度//种类 (基本 / 特技 / 物品)# 基本 (攻击 / 防御 / 逃跑)
	 * # 特技 ID# 物品 ID # 对像索引 # 強强制标志
	 */
	public class BattleAction {
		private var battler:Battler;
		public var speed : Number;                   
		public var kind : int,basic : int ;                    
		public var skill_id : int,  item_id : int ,targetIndex : int ; 
		public var actTargets:Array;           		
		public var forcing : Boolean                 
	//	private var db:DataBase
		public function BattleAction(battler:Battler){
			this.battler=battler;
	//		db=DataBase.getInstance();
			init();
		}
		
		
		public function get isAttack():Boolean{
			return (kind==0&&basic==0);
		}
		
		public function get isSkill():Boolean{
			return kind==1;
		}
		
		public function get isItem():Boolean{
			return kind==2;
		}
		
		public function get isGuard():Boolean{
			return (kind==0&&basic==1);
		}
		
		public function get isNothing():Boolean{
			return (kind==0&&basic<0);
		}
		
		
		
/**
		 * 判断行动有效度
     在事件命令并不造成强制性战斗行动时，因状态限制或缺少物品而导致
    无法行动，则返回 false.
  */
		public function get isValid():Boolean{
			
			if (isNothing){
				return false 
			}
		//	return true if @forcing                       # 强制行动
			
			// 无法行动
			if (!battler.movable){
				return false;
			}
				
			       
//			if skill?                                     # 技能
//			return false unless battler.skill_can_use?(skill)
//			elsif item?                                   # 物品
//			return false unless friends_unit.item_can_use?(item)
//			end
			return true;
		}
		
		
		private function init():void{
			clear();
		}
		
		public function clear():void{
			speed = 0
		    kind = 0
		    basic =-1
		    skill_id = 0
		    item_id = 0
		    targetIndex = -1
		    forcing = false;
			actTargets=null;
		}
		
		public function setAttack():void{
			basic=0;
			kind=0;
		}
		
		public function setGuard():void{
			basic=1;
			kind=0;
		}
		
		public function setSkill(skillId):void{
			kind = 1;
    		skill_id = skillId;
		}
		
		public function setItem(itemId):void{
			kind = 2;
    		item_id = itemId;
		}
		
		
		
	// 确认行动速度
		public function make_speed():void{
				
		    speed = battler.agi + Math.random()*(5 + battler.agi / 4);
			
		   // speed += 2000 if guard?
		    //speed += 1000 if attack? and battler.fast_attack
		}
   
		
		public function decideRandomTarget(friendTroop:Troop,oppTroop:Troop):void{
		
		}
		
		
		
		
		public function  make_targets():Array{
			var targets:Array;
  		  if (true||isAttack){
			  //现在全是攻击
  		  	targets=make_attack_targets();
  		  	trace("选中攻击的目标敌人"+targets)
  		  }
  		  actTargets=targets;
      		return targets;
   
		}
		//生成普通攻击目标
		private function  make_attack_targets():Array{
			var targets:Array = [];
			// 连续攻击
//			 if (battler.dual_attack){
//			 	targets += targets;
//			 }     
      		// targets.push(battler.opponents_unit.randomTarget());
      	//	targets.push(battler.opponentTroop.smoothTarget(targetIndex));
			var enemys:Array=battler.opponentsTroop.members
			for (var i:int=0;i<enemys.length;i++){
				var enemy:Unit=enemys[i]
				if (battler.canAttack(enemy)){
					targets.push(enemy);
					break;
				}
				
			}
 			return targets;
		}
    
   		
	}
}
